package com.main;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Animation {

	public Sprite[] animate;
	public Sprite activeSprite;
	public int xpos;
	public int ypos;
	private int index;
	private int lived;
	private int life;
	private int delay;
	private int timer;
	private int animationspeed;
	public boolean alive;
	private boolean running;

	public Animation(int posX, int posY, Sprite[] toanimate, int lifetime,int delay){
		xpos = posX;
		ypos = posY;
		lived = 0;
		life = lifetime;
		this.delay = delay;
		running = false;
		alive = true;
		timer = 0;
		animate = toanimate;
		activeSprite = animate[0];
		index = 0;
	}
	public void updatePos(int x, int y){
	xpos=x;
	ypos=y;
	}

	public void setNextSprite(){

		if (animationspeed > 30){
			timer += Gdx.graphics.getDeltaTime()*1000;
			if( timer > delay)
				running = true;
			if(alive){
				activeSprite = animate[index];
				index++;		

				if(index == animate.length-1)
					index = 0;

				if(running){
					lived++;
					if(lived == life)
						alive = false;
				}
			}
			animationspeed = 0;
		}
		else{
			animationspeed += Gdx.graphics.getDeltaTime()*1000;
		}

	}


	public void render(SpriteBatch sb){
		if(running){
			activeSprite.setPosition(xpos*Game.tileWidth,ypos*Game.tileHeight);
			activeSprite.draw(sb);
		}
	}
}
